#!/bin/sh
#
# Configuration variables
#
# JAVA_HOME
#   Home of Java installation.
#
# JAVA_OPTIONS
#   Extra options to pass to the JVM
#

usage()
{
    echo "Usage: $0 action [properties]"
    echo "where supported actions are:"
    echo "  run          Run longwell"
    echo "  debug        Run longwell and turn on JVM remote debug"
    echo "  profile      Run longwell and turn on JVM profiling with HProf (Sun JVM only)"
    echo "  shark        Run longwell and turn on JVM profiling with Apple Shark (MacOSX only)"
    exit 1
}

[ $# -gt 0 ] || usage

ACTION=$1
shift
ARGS="$*"

# ----- Verify and Set Required Environment Variables -------------------------

if [ "$JAVA_HOME" = "" ] ; then
  echo You must set JAVA_HOME to point at your Java Development Kit installation
  exit 1
fi

if [ "$JAVA_OPTIONS" = "" ] ; then
  JAVA_OPTIONS='-Xms32M -Xmx512M'
fi

if [ "$JAVA_DEBUG_ARGS" = "" ] ; then
  JAVA_DEBUG_ARGS='-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n'
fi

if [ "$JAVA_PROFILE_ARGS" = "" ] ; then
  JAVA_PROFILE_ARGS='-Xrunhprof:heap=all,cpu=samples,thread=y,depth=3'
fi

if [ "$JAVA_SHARK_ARGS" = "" ] ; then
  JAVA_SHARK_ARGS='-Xrunshark'
fi

# ----- Set platform specific variables

PATHSEP=":";
case "`uname`" in
   CYGWIN*) PATHSEP=";" ;;
esac

# ----- Set Local Variables ( used to minimize cut/paste) ---------------------

JAVA="$JAVA_HOME/bin/java"
PARSER="-Dorg.xml.sax.parser=org.apache.xerces.parsers.SAXParser"
LOADER="Loader"
LOADER_LIB="./tools/loader/loader.jar"
LONGWELL="-Dloader.main.class=edu.mit.simile.longwell.Main"
LONGWELL_LIBRARIES="-Dloader.jar.repositories=build/eclipse:build/classes:lib"
LONGWELL_PROPERTIES="longwell.properties"

# ----- Do the action ----------------------------------------------------------

case "$ACTION" in
  run)
        $JAVA $JAVA_OPTIONS -cp $LOADER_LIB $PARSER $LONGWELL_LIBRARIES $LONGWELL $LOADER $ARGS
        ;;

  debug)
        $JAVA $JAVA_OPTIONS $JAVA_DEBUG_ARGS -cp $LOADER_LIB $PARSER $LONGWELL_LIBRARIES $LONGWELL $LOADER $ARGS
        ;;

  profile)
        $JAVA $JAVA_OPTIONS $JAVA_PROFILE_ARGS -cp $LOADER_LIB $PARSER $LONGWELL_LIBRARIES $LONGWELL $LOADER $ARGS
        ;;

  shark)
        $JAVA $JAVA_OPTIONS $JAVA_SHARK_ARGS -cp $LOADER_LIB $PARSER $LONGWELL_LIBRARIES $LONGWELL $LOADER $ARGS
        ;;

  *)
        usage
        ;;
esac

exit 0
